/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  IMFEventListSingleton.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 20/02/14.
//  Copyright (c) 2014 redia. All rights reserved.
//

#import "BAEventsCache.h"
#import "BAEvent.h"

@interface BAEventsCache(){
    BOOL isFetching;
}

@property (nonatomic, strong) NSArray* cachedEvents;
@property (nonatomic, strong) NSDictionary* cachedEventsDict;
@property (nonatomic, strong) NSMutableArray* resultBlocks;
@property (nonatomic, strong) NSMutableArray* errorBlocks;

@end

@implementation BAEventsCache
SINGLETON_IMPL_GCD(BAEventsCache)

-(id)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(libraryChanged:) name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
}

-(void)libraryChanged:(id)sender
{
    self.cachedEvents = nil;
    self.cachedEventsDict = nil;
}

typedef void(^ResultBlock)(NSArray *, NSDictionary*);
typedef void(^ErrorBlock)(int, NSString *);

-(void)getEventsWithResultBlock:(void (^)(NSArray *, NSDictionary*))resultBlock errorBlock:(void (^)(int, NSString *))errorBlock{
    if (self.cachedEvents != nil && self.cachedEventsDict != nil) {
        resultBlock(self.cachedEvents, self.cachedEventsDict);
        return;
    }
    
    if (self.resultBlocks == nil) {
        self.resultBlocks = [NSMutableArray new];
    }
    if (self.errorBlocks == nil) {
        self.errorBlocks = [NSMutableArray new];
    }
    [self.resultBlocks addObject:resultBlock];
    [self.errorBlocks addObject:errorBlock];
    
    if (isFetching) {
        return;
    }
    
    isFetching=YES;
    
    [BARPCManager loadAllEventsFromView:self
                            resultBlock:^(NSArray *results) {
                                isFetching = NO;
                                self.cachedEvents = results;
                                NSMutableDictionary* eventsDict = [NSMutableDictionary new];
                                for (BAEvent* event in results) {
                                    [eventsDict setObject:event forKey:event.identifier];
                                }
                                self.cachedEventsDict = [NSDictionary dictionaryWithDictionary:eventsDict];
                                for (ResultBlock block in self.resultBlocks) {
                                    block(results, self.cachedEventsDict);
                                }
                                self.resultBlocks = nil;
                                self.errorBlocks = nil;
                            } errorBlock:^(int errorCode, NSString *errorMessage) {
                                DLog(@"ERROR loadAllEventsFromView: %@",errorMessage);
                                isFetching = NO;
                                for (ErrorBlock block in self.errorBlocks) {
                                    block(errorCode, errorMessage);
                                }
                                self.resultBlocks = nil;
                                self.errorBlocks = nil;
                            }];
}

-(void)refreshEventsWithResultBlock:(void (^)(NSArray *, NSDictionary*))resultBlock errorBlock:(void (^)(int, NSString *))errorBlock{
    self.cachedEvents = nil;
    self.cachedEventsDict = nil;
    [self getEventsWithResultBlock:resultBlock errorBlock:errorBlock];
}

@end
